//
//  XMDate+Extension.swift
//  AnneKit-Swift
//
//  Created by 寜小陌 on 2021/3/3.
//  Copyright © 2021 宁小陌. All rights reserved.
//

import UIKit

/// 60s
public let xm_minute : Double = 60 ;
/// 3600s
public let xm_hour : Double   = 3600 ;
/// 86400
public let xm_day : Double    = 86400 ;
/// 604800
public let xm_week : Double   = 604800 ;
/// 31556926
public let xm_year : Double   = 31556926 ;

enum XMDateFormatter:String{
    case kFormatterMD       = "MM-dd"
    case kFormatterMDHM     = "MM-dd HH:mm"
    case kFormatterMDHMS    = "MM-dd HH:mm:ss"
    
    case kFormatterHM       = "HH:mm"
    case kFormatterHMS      = "HH:mm:ss"
    
    case kFormatterY        = "yyyy"
    case kFormatterYM       = "yyyy-MM"
    case kFormatterYMD      = "yyyy-MM-dd"
    case kFormatterYMDHMS   = "yyyy-MM-dd HH:mm:ss"
    case kFormatterYMDHMSSS = "yyyy-MM-dd HH:mm:ss SSS"
}

/// 获取当前时间字符串
/// - Parameter mode: mode
func nxm_getCurrentStingTime(_ formatter:XMDateFormatter = .kFormatterYMDHMS) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = formatter.rawValue
    let dataString = dateFormatter.string(from: Date())
    return dataString
}

/// 获取当前时间戳
/// - Parameter isMS: 是否是毫秒级的
func nxm_getCurrentStampTime(isMS:Bool = true) ->Int{
    let dateFormatter = DateFormatter()
    var format = XMDateFormatter.kFormatterMDHMS
    if isMS == false {
        format = XMDateFormatter.kFormatterMDHM
    }
    dateFormatter.dateFormat = format.rawValue
    let dataString = dateFormatter.string(from: Date())
    let date = dateFormatter.date(from: dataString)
    let stamp = date!.timeIntervalSince1970
    
    return isMS ? Int(stamp * 1000) : Int(stamp)
}

/// 时间戳转字符串
/// - Parameter stamp: stamp description
func nxm_toStringTime(stamp:Int,_ mode:XMDateFormatter = .kFormatterYMDHMS) -> String {
    let timeInterval:TimeInterval = TimeInterval(stamp)
    let date = Date(timeIntervalSince1970: timeInterval)
    let dateformatter = DateFormatter()
    dateformatter.dateFormat = mode.rawValue
    let dateString = dateformatter.string(from: date)
    
    return dateString
}


/// 字符串转时间戳
/// - Parameter strTime: strTime description
func nxm_toStampTime(strTime:String,_ mode:XMDateFormatter = .kFormatterYMDHMS) -> Int {
    let dateFormatter = DateFormatter.init()
    dateFormatter.dateFormat = mode.rawValue
    let current = dateFormatter.date(from: strTime)
    let date = Date.init(timeInterval: 0, since: current ?? Date())
    let stamp = date.timeIntervalSince1970
    return Int(stamp)
}


/// 时间字符串转自定义时间格式字符串
/// - Parameters:
///   - customFormat: 自定义格式字符串 例如:@"HH:mm MM/dd"
///   - timeStr: 原始时间格式字符串
///   - oldFormat: 老的时间格式，默认是：yyy-MM-dd HH:mm:ss
func nxm_getCustomFormatTime(customFormat:String,timeStr:String,oldFormat:XMDateFormatter = .kFormatterYMDHMS) -> String {
    
    let fmt = DateFormatter()
    fmt.dateFormat = oldFormat.rawValue
    let date = fmt.date(from: timeStr)
    let customfmt = DateFormatter()
    customfmt.dateFormat = customFormat
    let resultStr = customfmt.string(from: date!)
     
    return resultStr
}

extension Date{
    // MARK: - 获取当前时区Date
    static func nxm_getCurrentDate() -> Date {
        let nowDate = Date()
        let zone = NSTimeZone.system
        let interval = zone.secondsFromGMT(for: nowDate)
        let localeDate = nowDate.addingTimeInterval(TimeInterval(interval))
        return localeDate
    }
}
